package pais.db;

import java.io.IOException;

import papos.rowset.DuplicatedKeyValueException;
import papos.rowset.KeyNameNotFoundException;
import papos.rowset.StorageSystem;
import pasys.error.PaposException;


public class HtmlPageTable implements TableInterface {
  private static HtmlPageTable INST_ = null;

  public static HtmlPageTable createInstance( String path ) {
    assert( INST_ == null );
    INST_ = new HtmlPageTable(path);
    return INST_;
  }
  public static HtmlPageTable getInstance() {
    return INST_;
  }

  private String path_;
  private StorageSystem storage_;
  public HtmlPageTable(String path) {
    path_ = path;
  }

  public void open(boolean writable) throws IOException {
    HtmlPage sampleInstance = new HtmlPage();

    try {
      storage_ = StorageSystem.open(	path_, 
          "html-page-table", 
          sampleInstance, 
          sampleInstance.getKeyDesc(),
          writable);
    } catch (IOException e) {
      throw e;
    } catch (Exception e) {
      e.printStackTrace();
      PAISDB.exit("While Opening the html-page-table : "+e.toString());
    }
  }
  public void sync() throws IOException, PaposException {
    storage_.sync();
  }

  public void close() throws IOException, PaposException {
    StorageSystem.close(storage_);
    storage_ = null;
  }

  public HtmlPage insert(long UID, String URL, long changeDate) throws PaposException {
    HtmlPage page = new HtmlPage(UID, URL, "No Title", changeDate, 0);
    try {
      if ( ! storage_.insert(page) ) {
        PAISDB.exitPrintingStack("Failed to insert a HTML page.(UID="+page.getUID()+")");
      }
    } catch (DuplicatedKeyValueException e) {
      e.printStackTrace();
      PAISDB.exit("Duplicated Keyvalue While Inserting a HTML page (UID="+page.getUID()+"): "+e.toString());
    }
    return page;
  }    
  public void update(HtmlPage page) throws PaposException {
    if ( ! storage_.update(page) ) {
      PAISDB.exitPrintingStack("Failed to update a HTML page.(UID="+page.getUID()+")");
    }
  }
  public void delete(HtmlPage page) throws PaposException {
    if ( ! storage_.delete(page) ) {
      PAISDB.exit("Failed to delete a HTML page.(UID="+page.getUID()+")");
    }
  }
  public StorageSystem getStorage() {
    return storage_;
  }

  public HtmlPage getPage(String URL) throws PaposException {
    HtmlPage page = new HtmlPage();
    page.setURL( URL );
    try {
      if ( ! storage_.find("URL_", page) ) {
        return null;
      }
    } catch (KeyNameNotFoundException e) {
      assert(false);
    }
    return page;
  }

  public HtmlPage getPage(long UID) throws PaposException {
    HtmlPage page = new HtmlPage();
    page.setUID( UID );
    try {
      if ( ! storage_.find("UID_", page) ) {
        return null;
      }
    } catch (KeyNameNotFoundException e) {
      assert(false);
    }
    return page;
  }

  public long getUID(String URL) throws PaposException {
    return getPage(URL).getUID();
  }

  public String getURL(long UID ) throws PaposException {
    return getPage(UID).getURL();
  }
}
